﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ZhaoLuKaoGuanWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client1 = new EmpbasicService.EmpBasicServiceClient();
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private KaoGuanModel model = new KaoGuanModel();
        public bool IsNew = false;
        public int ID;
        public ZhaoLuKaoGuanWindow(int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增考官信息";
                model = new KaoGuanModel();
                this.DataContext = model;
                //nian.SelectedIndex = 0;
            }
            else
            {
                this.Title = "编辑考官信息";
                Client.GetKaoGuanModelCompleted +=
                    new EventHandler<GetKaoGuanModelCompletedEventArgs>(Client_GetKaoGuanModelCompleted);
            }
        }

        void Client_GetKaoGuanModelCompleted(object sender, GetKaoGuanModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
            if (model.IsKaoGuanZheng = true) check.IsChecked = true; else check.IsChecked = false;
            //nian.SelectedIndex = DateTime.Today.Year - model.Year;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(model.YongRenDanWei))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入用人单位！");
                return;
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入姓名！");
                return;
            }
            if (string.IsNullOrEmpty(model.CardNo))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入身份证号！");
                return;
            }
            if (!string.IsNullOrEmpty(model.RiQi.ToString()) && model.RiQi != Convert.ToDateTime("1900-01-01"))
            {
                if (Convert.ToDateTime(model.RiQi).AddYears(3) < DateTime.Today)
                {
                    model.Memo += "\r\n考官证已过期";
                }
            }
            if (IsNew == true)
            {
                //model.Year = DateTime.Today.Year - nian.SelectedIndex;
                model.Creator = Session.LoginUser.LoginName;
                model.Type = "招录工作";
                if (check.IsChecked == true) model.IsKaoGuanZheng = true; else model.IsKaoGuanZheng = false;
                Client.AddKaoGuanModelCompleted += new EventHandler<AddKaoGuanModelCompletedEventArgs>(Client_AddKaoGuanModelCompleted);
                Client.AddKaoGuanModelAsync(model);
            }
            else
            {
                //model.Year = DateTime.Today.Year - nian.SelectedIndex;
                if (check.IsChecked == true) model.IsKaoGuanZheng = true; else model.IsKaoGuanZheng = false;
                Client.UpdateKaoGuanModelCompleted += new EventHandler<UpdateKaoGuanModelCompletedEventArgs>(Client_UpdateKaoGuanModelCompleted);
                Client.UpdateKaoGuanModelAsync(model);
            }
        }

        void Client_UpdateKaoGuanModelCompleted(object sender, UpdateKaoGuanModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void Client_AddKaoGuanModelCompleted(object sender, AddKaoGuanModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
